package ar.com.fiuba.ElectronicCircuitAnalyst.domain.impl.functions.expressions;

import java.util.List;

import ar.com.fiuba.ElectronicCircuitAnalyst.domain.impl.functions.connectors.Connector;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.impl.functions.exceptions.InvalidExpressionException;

/**
 * Class that represents the combination of two expressions with a logical
 * connector.
 * 
 */
public class CompositeExpression extends GenericExpression {

	private GenericExpression leftExpression;

	private GenericExpression rightExpression;

	private Connector connector;

	/**
	 * Constructor.
	 * 
	 * @param expressionName
	 *            name for the expression.
	 * @param leftExpression
	 *            first expression to be applied.
	 * @param rightExpression
	 *            second expression to be applied.
	 * @param connector
	 *            connector to connect both expressions.
	 * @throws InvalidExpressionException
	 *             if an invalid parameter is provided, such as a null value.
	 */
	public CompositeExpression(String expressionName, GenericExpression leftExpression,
			GenericExpression rightExpression, Connector connector) throws InvalidExpressionException {

		if (expressionName == null)
			throw new InvalidExpressionException("The expression hasn't got a Name.");
		this.expressionName = expressionName;

		if (connector == null)
			throw new InvalidExpressionException("The expression hasn't got a connector.");
		this.connector = connector;

		if (leftExpression == null)
			throw new InvalidExpressionException("The left child of the CompositeExpression is null.");
		this.leftExpression = leftExpression;

		if (rightExpression == null)
			throw new InvalidExpressionException("The right child of the CompositeExpression is null.");
		this.rightExpression = rightExpression;

	}

	/**
	 * {@link GenericExpression#evaluate(List)}
	 */
	public final Boolean evaluate(List<Boolean> inputs) {
		return connector.evaluate(leftExpression.evaluate(inputs), rightExpression.evaluate(inputs));
	}

}
